//
//  API_Codable_Demo1VC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/11/11.
//

import UIKit

struct API_Codable_Student {
    var name: String
    var age: Int
    var weight: Float
}

extension API_Codable_Student : Codable {
    
    /// 自定义字段属性
    /// 注意 1.需要遵守`Codingkey`
    ///     2.每个字段都要枚举
    enum Codingkeys: String, CodingKey {
        case name = "nickname"
        case age
        case weight
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: Codingkeys.self)
        self.name = try container.decode(String.self, forKey: .name)
        self.age = try container.decode(Int.self, forKey: .age)
        self.weight = try container.decode(Float.self, forKey: .weight)
    }
}

class API_Codable_Demo1VC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
//        testJSONToModel()
//        
//        testModelToJSON()
        
        testJSONToModel_CodingKey()
    }
}

//!!!: CodingKey
extension API_Codable_Demo1VC {
    private func testJSONToModel_CodingKey() {
        let jsonString =
        """
        {
            "nickname": 34,
            "age": 12,
            "weight": 43.2
        }
        """
        guard let data = jsonString.data(using: .utf8) else { return }
//        if let xm = try? JSONDecoder().decode(API_Codable_Student.self, from: data) {
//            debugPrint("CodingKey log is \(xm.name), \(xm.age), \(xm.weight)")
//        } else {
//            debugPrint("CodingKey 解析失败啦啦啦----")
//        }
        
        do {
            let xiaoMing = try JSONDecoder().decode(API_Codable_Student.self, from: data)
            debugPrint("xiaoMing log is \(xiaoMing.name), \(xiaoMing.age), \(xiaoMing.weight)")
        } catch {
            debugPrint("xiaoMing解析失败啦啦啦----\(error)")
        }
    }
}

//!!!: 基础tests
extension API_Codable_Demo1VC {
    private func testJSONToModel() {
        let jsonString =
        """
        {
            "name": "小明",
            "age": 12,
            "weight": 43.2
        }
        """
        guard let data = jsonString.data(using: .utf8) else { return }

        /// 方式一
        do {
            let xiaoMing = try JSONDecoder().decode(API_Codable_Student.self, from: data)
            debugPrint("xiaoMing log is \(xiaoMing.name), \(xiaoMing.age), \(xiaoMing.weight)")
        } catch {
            debugPrint("xiaoMing解析失败啦啦啦----\(error)")
        }

        /// 方式二
        if let xm = try? JSONDecoder().decode(API_Codable_Student.self, from: data) {
            debugPrint("xm log is \(xm.name), \(xm.age), \(xm.weight)")
        } else {
            debugPrint("xm解析失败啦啦啦----")
        }

    }

    private func testModelToJSON() {
        let stu = API_Codable_Student(name: "jianbo.pei", age: 18, weight: 75)

        /// 方式一
        do {
            let data = try JSONEncoder().encode(stu)
            let jsonString = String(data: data, encoding: .utf8)
            debugPrint("encoder do catch jsonString is \(String(describing: jsonString))")
        } catch {
            debugPrint("encoder do catch 失败")
        }

        /// 方式二
        if let data = try? JSONEncoder().encode(stu) {
            let jsonString = String(data: data, encoding: .utf8)
            debugPrint("encoder try? jsonString is \(String(describing: jsonString))")
        } else {
            debugPrint("encoder try? 失败")
        }
    }
}
